import {
  queryLogStatic,
  queryTopCount,
  queryTopAvgTime,
  getCountData,
  getAvgTimeData,
  reportStatistic,
} from '@/services/sqlRunLogStatic';

export default {
  namespace: 'sqlRunLogStatic',

  state: {
    list: [],
    currentUser: {},
    data: [],
    reportStatistic: {},
  },

  effects: {
    *fetchByParams({ payload }, { call, put }) {
      const resp = yield call(queryLogStatic, payload);
      yield put({
        type: 'saveLogStatic',
        payload: resp.data,
      });
    },
    *getCountData({ payload }, { call, put }) {
      const resp = yield call(getCountData, payload);
      yield put({
        type: 'saveCountData',
        payload: resp.data,
      });
    },
    *getAvgTimeData({ payload }, { call, put }) {
      const resp = yield call(getAvgTimeData, payload);
      yield put({
        type: 'saveAvgTimeData',
        payload: resp.data,
      });
    },
    *getTopCount({ payload }, { call, put }) {
      const resp = yield call(queryTopCount, payload);
      yield put({
        type: 'saveTopCount',
        payload: resp.data,
      });
    },
    *getTopAvgTime({ payload }, { call, put }) {
      const resp = yield call(queryTopAvgTime, payload);
      yield put({
        type: 'saveTopAvgTime',
        payload: resp.data,
      });
    },
    *fetchReportStatistic({ payload }, { call, put }) {
      const resp = yield call(reportStatistic, payload);
      yield put({
        type: 'saveReportInfo',
        payload: resp.data,
      });
    },
  },

  reducers: {
    saveLogStatic(state, action) {
      return {
        ...state,
        data: action.payload,
      };
    },
    saveCountData(state, action) {
      return {
        ...state,
        countData: action.payload,
      };
    },
    saveAvgTimeData(state, action) {
      return {
        ...state,
        avgTimeData: action.payload,
      };
    },
    saveTopCount(state, action) {
      return {
        ...state,
        topCount: action.payload,
      };
    },
    saveTopAvgTime(state, action) {
      return {
        ...state,
        topAvgTime: action.payload,
      };
    },
    saveReportInfo(state, action) {
      return {
        ...state,
        reportStatistic: action.payload,
      };
    },
  },
};
